<?php
namespace addons\Asset\common\models;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%addon_asset}}".
 *
 * @property int $id 编号
 * @property int $cate_id 分类
 * @property string $title 标题
 * @property string $content 内容
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Asset extends ActiveRecord
{

    public function behaviors()
    {
        return [

            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_asset}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cate_id','serial','title','cate_id', 'purchase_date', 'num','purchase_price'], 'required'],
            [['merchant_id','cate_id','num', 'status'], 'integer'],
            [['title', 'department', 'address', 'serial', 'unit','person','picture'], 'string', 'max' => 255],
            [['purchase_price'], 'number', 'min' => 0],
            [['serial'], 'unique', 'filter' => function (ActiveQuery $query) {
                return $query->all();
            }],
            [['covers','created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '名称',
            'cate_id' => '分类',
            'num' => '数量',
            'unit' => '单位',
            'serial' => '编号',
            'department' => '使用部门',
            'address' => '使用地点',
            'purchase_date' => '购买日期',
            'purchase_price' => '单价',
            'person' => '负责人',
            'picture' => '封面',
            'covers' => '图片',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
    public function getCate()
    {
        return $this->hasOne(AssetCate::class, ['id' => 'cate_id']);
    }
    /**
     * 资产下拉
     *
     * @return \yii\db\ActiveQuery
     */
    public static function getDropDown()
    {
        $models = self::find()
            ->select(['id', 'title'])
//            ->where(['>','num',0])
            ->orderBy('num desc,title asc')
            ->asArray()
            ->all();
        return ArrayHelper::map($models, 'id', 'title');
    }
    public static function shopDropDown()
    {
        $models = self::find()
            ->select(['id', 'title'])
            ->orderBy('num desc,title asc')
            ->asArray()
            ->all();
        return ArrayHelper::map($models, 'id', 'title');
    }
    public function beforeSave($insert)
    {
        $this->merchant_id = Yii::$app->user->identity->merchant_id;
        // 更新系统主图
        $covers = Json::decode($this->covers);
        $this->picture = $covers[0] ?? '';
        return parent::beforeSave($insert);
    }


}
